class Solution(object):
    def isValidSerialization(self, preorder):
        """
        :type preorder: str
        :rtype: bool
        """
        preorder = preorder.split(',')
        diff = 1
        for e in preorder:
            diff-=1
            if diff<0: return False
            if e!='#': diff+=2
        return diff==0